import scipy
from scipy.io import loadmat

def mat2txt():
	mat = loadmat('S1.mat')
	print(mat.keys())

	print(mat['data'].shape, mat['annotation_data'].shape)
	print(mat['data'][:3])
	print(mat['annotation_data'][:3])

	data = mat['data']
	anno = mat['annotation_data']

	data_str = ''
	for i in range(data.shape[0]):
		data_str += '{} {}\n'.format(data[i,0], data[i,1])
	with open('S1_data.txt', 'w') as f:
		f.write(data_str)


	data_str = ''
	for i in range(anno.shape[0]):
		data_str += '{}\n'.format(anno[i,0])
	with open('S1_anno.txt', 'w') as f:
		f.write(data_str)
# mat2txt()


# import random
# data_str = ''
# for i in range(3000):
# 	data_str += '{} {}\n'.format(random.randint(0, 9999), random.randint(0, 9999))
# # print(data_str)
# with open('demo.txt', 'w') as f:
# 	f.write(data_str)

def turn_tb1m():
	mat = loadmat('data_TB1M.mat')
	print(mat.keys())
	print(mat['fea'].shape, mat['gt'].shape)
	data = mat['fea']
	anno = mat['gt']

	data_str = ''
	for i in range(data.shape[0]):
		data_str += '{} {}\n'.format(data[i,0], data[i,1])
	with open('tb1m_data.txt', 'w') as f:
		f.write(data_str)


	# data_str = ''
	# for i in range(anno.shape[0]):
	# 	data_str += '{}\n'.format(anno[i,0])
	# with open('tb1m_anno.txt', 'w') as f:
	# 	f.write(data_str)


def mat2txt_2gauss2m():
	mat = loadmat('TwoGauss_2M.mat')
	print(mat.keys())

	print(mat['data'].shape, mat['annotation_data'].shape)
	print(mat['data'][:3])
	print(mat['annotation_data'][:3])

	data = mat['data']
	anno = mat['annotation_data']

	data_str = ''
	for i in range(data.shape[0]):
		data_str += '{} {}\n'.format(data[i,0], data[i,1])
	with open('TwoGauss_2M_data.txt', 'w') as f:
		f.write(data_str)


	data_str = ''
	for i in range(anno.shape[0]):
		data_str += '{}\n'.format(anno[i,0])
	with open('TwoGauss_2M_anno.txt', 'w') as f:
		f.write(data_str)


# mat = loadmat('tb1m_matlab.mat')
# print(mat.keys())
# print(mat['pred'].shape)

def mat2txt_func(mat_name='TwoGauss_20M_2clusters'):
	mat = loadmat(mat_name+'.mat')
	print(mat.keys())

	print(mat['data'].shape, mat['annotation_data'].shape)
	# print(mat['data'][:3])
	# print(mat['annotation_data'][:3])

	data = mat['data']
	anno = mat['annotation_data']

	data_str = ''
	for i in range(data.shape[0]):
		data_str += '{} {}\n'.format(data[i,0], data[i,1])
	with open(mat_name+'_data.txt', 'w') as f:
		f.write(data_str)


	data_str = ''
	for i in range(anno.shape[0]):
		data_str += '{}\n'.format(anno[i,0])
	with open(mat_name+'_anno.txt', 'w') as f:
		f.write(data_str)

mat2txt_func('TwoGauss_20M_2clusters')
mat2txt_func('TwoGauss_20M_20clusters')
mat2txt_func('TwoGauss_20M_200clusters')
